<?php
function annotate_help($path, $arg) {
  $output = '';  //declare your output variable
  switch ($path) {
    case "admin/help#annotate":
      $output = '<p>'.  t("Displays links to nodes created on this date") .'</p>';
      break;
  }
  return $output;
} // function annotate_help

function annotate_perm() {
  return array('access annotate content', 'administer annotate');
} // function annotate_perm()

function annotate_block($op='list', $delta=0) {

	if ($op == "list") {
		// Generate listing of blocks from this module, for the admin/block page
		$block = array();
		$block[0]["info"] = t('On This Date');
		return $block;
	}
	else if ($op == 'view') {

		// Generate our block content

		// content variable that will be returned for display
		$block_content = '';

		// Get today's date
		$today = getdate();

		// calculate midnight one week ago
		$start_time = mktime(0, 0, 0,$today['mon'],
		($today['mday'] - 1), $today['year']);

		// we want items that occur only on the day in question, so
		//calculate 1 day
		$end_time = $start_time + 864000;
		// 60 * 60 * 24 = 86400 seconds in a day

		$limitnum = variable_get("onthisdate_maxdisp", 3);

		$query = "SELECT nid, title, created FROM " .
		"{node} WHERE created >= %d " .
		"AND created <= %d";

		$query_result = db_query_range($query, $start_time, $end_time, 0, $limitnum);
		while ($links = db_fetch_object($query_result)) {
			$block_content .= l($links->title, 'node/'.$links->nid) . '<br />';
		}

		// check to see if there was any content before returning
		//  the block view
		if ($block_content == '') {
			// no content from a week ago, return nothing.
			return;
		}
		// set up the block
		$block['subject'] = 'On This Date';
		$options = array( "attributes" => array("title" => t("More events on this day.") ) );
		$link = l( t("more"), "annotate", $options );

		$block_content .= "<div class=\"more-link\">" . $link . "</div>";
		$block['content'] = $block_content;
		return $block;
	}
 
}  // end annotate_block

function annotate_admin() {
  $form = array();

  $form['onthisdate_maxdisp'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum number of links'),
    '#default_value' => variable_get('onthisdate_maxdisp', 3),
    '#size' => 2,
    '#maxlength' => 2,
    '#description' => t("The maximum number of links to display in the block."),
    '#required' => TRUE,
  );

  return system_settings_form($form);
}

function annotate_menu() {

  $items = array();

  $items['admin/settings/annotate'] = array(
    'title' => 'On this date module settings',
    'description' => 'Description of your On this date settings page',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('annotate_admin'),
    'access arguments' => array('access administration pages'),
    'type' => MENU_NORMAL_ITEM,
   );
	
   //this is added for this current tutorial.
  $items['annotate'] = array(
    'title' => 'On this date',
    'page callback' => 'annotate_all',
    'access arguments' => array('access annotate content'),
    'type' => MENU_CALLBACK
  );

  return $items;
}

function annotate_all() {
  // content variable that will be returned for display
  $page_content = '';

  // Get today's date
  $today = getdate();

  // calculate midnight one week ago
  $start_time = mktime(0, 0, 0, $today['mon'], ($today['mday'] - 1), $today['year']);

  // we want items that occur only on the day in question,
  // so calculate 1 day
  $end_time = $start_time + 864000;
  // 60 * 60 * 24 = 86400 seconds in a day

  $query = "SELECT nid, title, created FROM " .
           "{node} WHERE created >= '%d' " .
           " AND created <= '%d'";

  // get the links (no range limit here)
  $queryResult =  db_query($query, $start_time, $end_time);
  while ($links = db_fetch_object($queryResult)) {
    $page_content .= l($links->title, 'node/'.$links->nid) . '<br />';
  }

  // More coming....
  if ($page_content == '') {
    // no content from a week ago, let the user know
    $page_content = "No events occurred on this site on this date in history.";
  }
  return $page_content;
}